<?php

declare(strict_types=1);

namespace app\model;

use think\Model;
use think\model\concern\SoftDelete;
use think\model\Relation;

/**
 * @mixin think\Model
 */
class Post extends Model
{
  //

  public static $stausNameList = [
    0 => '草稿',
    1 => '发布'
  ];

  public static $verifyNameList = [
    0 => '下架',
    1 => '通过',
    2 => '审核中',
  ];

  public $readUser = null; // 浏览用户的人

  use SoftDelete;

  protected $defaultSoftDelete = 0;


  public function collect()
  {
    return $this->hasMany(PostCollect::class, 'post_id');
  }

  public function topic()
  {
    return $this->belongsTo(Topic::class, 'topic_id');
  }

  public function comments()
  {
    return $this->hasMany(PostComment::class, 'post_id');
  }

  public function invited()
  {
    return $this->hasMany(PostInvite::class, 'post_id');
  }

  public function categorys()
  {
    return $this->hasMany(PostCategory::class, 'post_id');
  }

  public function tags()
  {
    return $this->hasMany(PostTag::class, 'post_id');
  }

  public function category()
  {
    return $this->belongsTo(Category::class, 'category_id');
  }

  public function user()
  {
    return $this->belongsTo(User::class, 'user_id');
  }

  public function setPublishTimeAttr($value)
  {
    return strtotime($value);
  }
  public function getPublishTimeAttr($value)
  {
    if (empty($value)) {
      return $this->getData('create_time');
    }

    return $value;
  }
  public function getPublishTimeTextAttr()
  {

    $value = $this->getAttr('publish_time');
    return date('Y-m-d', $value);
  }
  public function getPublishTimeDatetimeAttr()
  {

    $value = $this->getAttr('publish_time');
    return date('Y-m-d H:i:s', $value);
  }
  public function getPublishTimeTranAttr()
  {

    $value = $this->getAttr('publish_time_datetime');
    return time_tran($value);
  }

  public function getCategorysListAttr()
  {
    $list_post_categorys = $this->getAttr('categorys');

    $list = array_column($list_post_categorys->append(['category'])->toArray(), 'category');

    $list = array2level($list, 0, 0);

    return $list;
  }

  public function getTagsListAttr()
  {
    $list_post_tags = $this->getAttr('tags');

    $list = array_column($list_post_tags->append(['tag'])->toArray(), 'tag');

    return $list;
  }

  public function getDescShortAttr()
  {
    $desc = $this->getData('desc');

    if (strlen($desc) > 100) {
      $desc = mb_substr($desc, 0, 100) . '...';
    }

    return $desc;
  }

  public function getDescListAttr()
  {
    $desc = $this->getData('desc');

    if (empty($desc)) {
      return '';
    }
    $list = explode("\n", $desc);

    return $list;
  }

  public function getDescHtmlAttr()
  {
    $desc = $this->getData('desc');

    if (empty($desc)) {
      return '';
    }

    return str_replace("\n", '<br>', $desc);
  }

  public function getStatusNameAttr()
  {
    return self::$stausNameList[$this->getData('status')];
  }
  public function getVerifyNameAttr()
  {
    return self::$verifyNameList[$this->getData('is_verify')];
  }

  public function setPubishTimeAttr($value)
  {
    return strtotime($value);
  }

  public function setContentAttr($value)
  {
    return json_encode($value);
  }
  public function setContentHtmlAttr($value)
  {
    return trim($value);
  }

  public function getContentAttr($value)
  {
    return json_decode($value, true);
  }

  public function getPosterAttr($value)
  {
    if (empty($value)) {
      $value = '/static/images/upload.png';
    }

    return get_source_link($value);
  }

  public static function searchPage($request)
  {
    $model_list_post = Post::where('type', 2)
      ->where('status', 1)
      ->where('is_verify', 1)
      ->order('sort desc');

    if (!empty($request->param('category_id'))) {
      $model_list_post->where('category_id', $request->param('category_id'));
    }
    if (!empty($request->param('is_good'))) {
      $model_list_post->where('is_good', $request->param('is_good'));
    }
    if (!empty($request->param('topic'))) {
      $model_topic = Topic::where('title', $request->param('topic'))->find();

      if (!empty($model_topic)) {
        $model_list_post->where('topic_id', $model_topic->id);
      }
    }

    $sort_by = $request->param('sort_by','create_time');

    if (!empty($sort_by)) {
      $model_list_post->order($sort_by, 'desc');
    }

    $list_post = $model_list_post->paginate([
      'list_rows'=>15,
      'query'=>$request->get()
    ]);


    return $list_post;
  }

  public function getIsLikeAttr()
  {
    if (is_null($this->readUser)) {
      return 0;
    }

    $model_zan = PostZan::where('user_id', $this->readUser->id)
      ->where('post_id', $this->getData('id'))
      ->find();

    if (empty($model_zan)) {
      return 0;
    }
    return 1;
  }
  public function getIsCollectAttr()
  {
    if (is_null($this->readUser)) {
      return 0;
    }

    $model_zan = PostCollect::where('user_id', $this->readUser->id)
      ->where('post_id', $this->getData('id'))
      ->find();

    if (empty($model_zan)) {
      return 0;
    }

    return 1;
  }

  public function getReadUrlAttr()
  {
    return url('Post/read', ['id' => $this->getData('id')]);
  }

  public function setFilesAttr($value)
  {
    return serialize($value);
  }

  public function getFilesAttr($value)
  {
    if (empty($value)) {
      return [];
    }
    return unserialize($value);
  }
}
